package hotel.switchs.oversea.biz.controller;

import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.api.VO.DltHotelEntityVo;
import hotel.switchs.oversea.api.VO.RequestParamVo;
import hotel.switchs.oversea.api.VO.request.*;
import hotel.switchs.oversea.biz.service.SwitchSubHotelInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author lmf
 * @Date 2020/5/15 10:59
 *  子酒店相关接口
 **/
@RestController
@Api(value = "SwitchStuHotelInfoController",tags = {"子酒店接口"})
@Slf4j
@RequestMapping("/switchStuHotelInfo")
public class SwitchStuHotelInfoController {
    Logger logger= LoggerFactory.getLogger(SwitchStuHotelInfoController.class);
    @Autowired
    private SwitchSubHotelInfoService switchSubHotelInfoService;

    @PostMapping("/getPageHotelIds")
    @ApiOperation(value = "分页获取子酒店id",notes="必须经过auth授权")
    public Result<Object> queryPageHotelIds(@RequestBody JSONObject jsonObject){
        //参数校验
        int index = jsonObject.get("index")==null?-1:(int)jsonObject.get("index");
        int pageSize = jsonObject.get("pageSize")==null?-1:(int)jsonObject.get("pageSize");
        if(-1 == index){
            return new Result<>("index不可为空");
        }
        if(-1 == pageSize){
            return new Result<>("pageSize不可为空");
        }
        //发起请求并保存数据
        return  switchSubHotelInfoService.queryPageIds(index,pageSize);
    }

    /**
     * 创建子酒店
     */
    @PostMapping("/createsubhotel")
    @ApiOperation(value = "创建子酒店接口",notes = "必须经过oauth2授权")
    public Result<Object> createSubHotel(@RequestBody RequestParamVo requestParamVo){
        Integer[] masterHotelID=requestParamVo.getMasterHotelIDs();
        if(masterHotelID.length<1){
            return new Result<>("母酒店ID不能为空!");
        }
        return switchSubHotelInfoService.createSubHoltel(masterHotelID);

    }
    /**
     * 获取子酒店列表
     */
    @PostMapping("/getdlthotellist")
    @ApiOperation(value = "获取子酒店列表接口",notes = "必须经过oauth2授权")
    public Result<Object> getSubHotelList(@RequestBody GetdlthotellistVo getdlthotellistVo){
        Map<String,Object> map=new HashMap<>();
        if(getdlthotellistVo.getHotelID()!=0){
            map.put("hotelID",getdlthotellistVo.getHotelID());
        }
        if(!StringUtils.isEmpty(getdlthotellistVo.getHotelName())){
            map.put("hotelName",getdlthotellistVo.getHotelName());
        }
        if(getdlthotellistVo.getMasterHotelID()!=0){
            map.put("masterHotelID",getdlthotellistVo.getMasterHotelID());
        }
        if(getdlthotellistVo.getCBookable()!=-1){
            map.put("cBookable",getdlthotellistVo.getCBookable());
        }
        if(getdlthotellistVo.getQBookable()!=-1){
            map.put("qBookable",getdlthotellistVo.getQBookable());
        }
        if(getdlthotellistVo.getChannelaBookable()!=-1){
            map.put("channelaBookable",getdlthotellistVo.getChannelaBookable());
        }
        if(getdlthotellistVo.getB2bBookable()!=-1){
            map.put("b2bBookable",getdlthotellistVo.getB2bBookable());
        }
        return switchSubHotelInfoService.getStuHotelList(getdlthotellistVo.getPageSize(),getdlthotellistVo.getPageIndex(),map);
    }
    @PostMapping("/saveHotelInfos")
    @ApiOperation(value = "保存子酒店信息接口",notes="必须经过auth授权")
    public Result<Object> saveHotelInfos(@RequestBody SwitchSubHotelRedisParamsVo switchSubHotelRedisParamsVo){
        //发起请求并保存数据
        Map<String,List<DltHotelEntityVo>> switchCityInfoVosMap = switchSubHotelRedisParamsVo.getSwitchHotelInfoVosMap();
        if(null == switchCityInfoVosMap || switchCityInfoVosMap.size()==0){
            return new Result<>("参数switchHotelInfoVosMap不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchSubHotelInfoService.saveHotelInfos(switchCityInfoVosMap);
    }
    /**
     * 设置子酒店售卖渠道
     */
    @PostMapping("/updatesupplierhotelbookablebybooktype")
    @ApiOperation(value = "设置子酒店售卖渠道",notes = "必须经过oauth2授权")
    public Result<Object> setSupplierHotel(@RequestBody UpdateChannelVo updateChannelVo) {
        Map<String, Object> map = new HashMap<>();
        //设置子酒店Id列表
        int[] hotelIDs = updateChannelVo.getHotelIDs();
        if (hotelIDs.length < 1) {
            return new Result<>("子酒店ID不能为空");
        }
        if(hotelIDs.length>50){
            return new Result<>("最多50个子酒店ID");
        }
        map.put("hotelIDs", hotelIDs);
        //用来标记是否有定时售卖
        boolean is_time = false;
        if (updateChannelVo.getCBookable() == 3 || updateChannelVo.getB2bBookable() == 3 || updateChannelVo.getQBookable() == 3 || updateChannelVo.getChannelABookable() == 3) {
            is_time = true;
        }
        map.put("cBookable", updateChannelVo.getCBookable());
        map.put("qBookable", updateChannelVo.getQBookable());
        map.put("channelABookable", updateChannelVo.getChannelABookable());
        map.put("b2bBookable", updateChannelVo.getB2bBookable());
        List<SupplierTimeSaleInfoVo> supplierTimeSaleInfoListVos = updateChannelVo.getSupplierTimeSaleInfoList();
        if (is_time) {
            if (ObjectUtils.isEmpty(supplierTimeSaleInfoListVos) || supplierTimeSaleInfoListVos.size() == 0) {
                return new Result<>("您设置的渠道有设置定时售卖，请填写定时售卖信息");
            }
            for (SupplierTimeSaleInfoVo supplierTimeSaleInfoListVo : supplierTimeSaleInfoListVos) {
                Pattern pattern = Pattern.compile("[0-1]*");
                if (supplierTimeSaleInfoListVo.getApplyWeek().length() != 7 || !pattern.matcher(supplierTimeSaleInfoListVo.getApplyWeek()).matches()) {
                    return new Result<>("试用星期必须只能是01组合的字符,长度为7位");
                }
            }
        }
        return switchSubHotelInfoService.setHotelSupplierBookable(map, supplierTimeSaleInfoListVos);
    }
    /**
     * 设置子酒店最晚预定时间
     */
    @PostMapping("/setdlthotelreservetime")
    @ApiOperation(value = "设置子酒店售最晚预定时间",notes = "必须经过oauth2授权")
    public Result<Object> setHotelReServeTime(@RequestBody SetHotelReServeTimeVo setHotelReServeTimeVo){
        logger.info("设置子酒店最晚预定时间");
        System.out.println(setHotelReServeTimeVo);
        Map<String,Object> map=new HashMap<>();
        int[] hotelIDs=setHotelReServeTimeVo.getHotelIDs();
        if(hotelIDs.length<1){
            return new Result<>("子酒店ID不能为空");
        }
        map.put("hotelIDs",hotelIDs);
        map.put("cReserveTime",setHotelReServeTimeVo.getCReserveTime());
        map.put("qReserveTime",setHotelReServeTimeVo.getQReserveTime());
        map.put("qReserveDay",setHotelReServeTimeVo.getQReserveDay());
        map.put("cReserveDay",setHotelReServeTimeVo.getCReserveDay());
        map.put("setCtrip",setHotelReServeTimeVo.isSetCtrip());
        map.put("setQunar",setHotelReServeTimeVo.isSetQunar());
        return switchSubHotelInfoService.setDhotelReServeTime(map);
    }

    /**
     * 获取子酒店可用属性值信息
     */
    @GetMapping("/getHotelAvailableProperties")
    @ApiOperation(value = "获取子酒店可用属性值信息",notes = "必须经过oauth2授权")
    public Result<Object> getHotelPropertiesList(@RequestParam(value = "hotelId")Integer hotelId){
        return switchSubHotelInfoService.getHotelAvailableProperties(hotelId);
    }


}
